package com.leyantech.ecp.agenx.common.utils;

import com.github.rholder.retry.RetryException;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-12-02.
 */
public class RetryUtils {

  static final Retryer<Object> COMMON_RETRYER = RetryerBuilder.newBuilder()
      .retryIfException()
      .withStopStrategy(StopStrategies.stopAfterAttempt(3))
      .withWaitStrategy(WaitStrategies.fixedWait(200, TimeUnit.MILLISECONDS))
      .build();

  public static <T> T retryCall(Callable<T> callable) throws ExecutionException, RetryException {
    return (T) COMMON_RETRYER.call((Callable) callable);
  }

}
